﻿using CoreCms.Net.Bz.Common.Response;
using CoreCms.Net.Bz.Filter;
using CoreCms.Net.Core.SilkyCore.Utils;
using CoreCms.Net.Core.SilkyCore.Utils.Expression;
using CoreCms.Net.Identity.HttpContextUser;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels;
using CoreCms.Net.Model.ViewModels.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace CoreCms.Net.Web.Admin.Controllers
{
    /// <summary>
    ///     后端常用方法
    /// </summary>
    [Description("后端常用方法")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ToolsController : ControllerBase
    {
        private readonly ICodeGeneratorServices _codeGeneratorServices;

        private readonly ISysMenuServices _sysMenuServices;
        private readonly ISysOrganizationServices _sysOrganizationServices;
        private readonly ISysRoleServices _sysRoleServices;
        private readonly ISysUserRoleServices _sysUserRoleServices;
        private readonly ISysRoleMenuServices _sysRoleMenuServices;
        private readonly ISysUserServices _sysUserServices;
        private readonly IHttpContextUser _user;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly ISysLoginRecordServices _sysLoginRecordServices;
        private readonly ISysNLogRecordsServices _sysNLogRecordsServices;

        private readonly ICoreCmsUserServices _userServices;

        private readonly ILogger _logger;

        /// <summary>
        ///     构造函数
        /// </summary>
        public ToolsController(
            IHttpContextUser user
            , IWebHostEnvironment webHostEnvironment

            , ISysUserServices sysUserServices
            , ISysRoleServices sysRoleServices
            , ISysMenuServices sysMenuServices
            , ISysUserRoleServices sysUserRoleServices
            , ISysOrganizationServices sysOrganizationServices, ICodeGeneratorServices codeGeneratorServices,
         ISysLoginRecordServices sysLoginRecordServices, ISysNLogRecordsServices sysNLogRecordsServices
            , ISysRoleMenuServices sysRoleMenuServices
            , ILogger<ToolsController> logger)
        {
            _user = user;
            _webHostEnvironment = webHostEnvironment;

            _sysUserServices = sysUserServices;
            _sysRoleServices = sysRoleServices;
            _sysMenuServices = sysMenuServices;
            _sysUserRoleServices = sysUserRoleServices;
            _sysOrganizationServices = sysOrganizationServices;
            _codeGeneratorServices = codeGeneratorServices;

            _sysLoginRecordServices = sysLoginRecordServices;
            _sysNLogRecordsServices = sysNLogRecordsServices;

            _sysRoleMenuServices = sysRoleMenuServices;

            _logger = logger;
        }

        #region 获取登录用户用户信息(用于面板展示)====================================================

        /// <summary>
        ///     获取登录用户用户信息(用于面板展示)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> GetUserInfo()
        {
            var jm = new AdminUiCallBack();
            var userModel = await _sysUserServices.QueryByIdAsync(_user.ID);
            jm.code = userModel == null ? 401 : 0;
            jm.msg = userModel == null ? "注销登录" : "数据获取正常";
            jm.data = userModel == null ? null : new
            {
                userModel.userName,
                userModel.nickName,
                userModel.createTime
            };
            return jm;
        }

        #endregion 获取登录用户用户信息(用于面板展示)====================================================

        #region 获取登录用户用户全部信息(用于编辑个人信息)====================================================

        /// <summary>
        ///     获取登录用户用户全部信息(用于编辑个人信息)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> GetEditUserInfo()
        {
            var jm = new AdminUiCallBack();
            var userModel = await _sysUserServices.QueryByIdAsync(_user.ID);

            if (userModel != null)
            {
                var roles = await _sysUserRoleServices.QueryListByClauseAsync(p => p.userId == userModel.id);
                if (roles.Any())
                {
                    var roleIds = roles.Select(p => p.roleId).ToList();
                    userModel.roles = await _sysRoleServices.QueryListByClauseAsync(p => roleIds.Contains(p.id));
                }

                if (userModel.organizationId != null && userModel.organizationId > 0)
                {
                    var organization = await _sysOrganizationServices.QueryByIdAsync(userModel.organizationId);
                    if (organization != null) userModel.organizationName = organization.organizationName;
                }
            }

            jm.code = 0;
            jm.msg = "数据获取正常";
            jm.data = userModel;
            return jm;
        }

        #endregion 获取登录用户用户全部信息(用于编辑个人信息)====================================================

        #region 获取角色列表信息====================================================

        /// <summary>
        ///     获取角色列表信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> GetManagerRoles()
        {
            var jm = new AdminUiCallBack();
            var roles = await _sysRoleServices.QueryAsync();
            jm.code = 0;
            jm.msg = "数据获取正常";
            jm.data = roles.Select(p => new { title = p.roleName, value = p.id });
            return jm;
        }

        #endregion 获取角色列表信息====================================================

        #region 用户编辑个人登录账户密码====================================================

        /// <summary>
        ///     获取登录用户用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> EditLoginUserPassWord([FromBody] FMEditLoginUserPassWord entity)
        {
            var jm = new AdminUiCallBack();

            if (string.IsNullOrEmpty(entity.oldPassword))
            {
                jm.msg = "请键入旧密码";
                return jm;
            }

            if (string.IsNullOrEmpty(entity.password))
            {
                jm.msg = "请键入新密码";
                return jm;
            }

            if (string.IsNullOrEmpty(entity.repassword))
            {
                jm.msg = "请键入新密码确认密码";
                return jm;
            }

            if (entity.password != entity.repassword)
            {
                jm.msg = "新密码与确认密码不相符";
                return jm;
            }

            if (entity.password == entity.oldPassword)
            {
                jm.msg = "请设置与旧密码不同的新密码";
                return jm;
            }

            var oldPassWord = CommonHelper.Md5For32(entity.oldPassword);
            var newPassWord = CommonHelper.Md5For32(entity.password);

            var userModel = await _sysUserServices.QueryByIdAsync(_user.ID);
            if (userModel.passWord.ToUpperInvariant() != oldPassWord)
            {
                jm.msg = "旧密码输入错误";
                return jm;
            }
            else if (userModel.passWord.ToUpperInvariant() == newPassWord)
            {
                jm.msg = "新旧密码一致，无需修改，请设置与旧密码不同的新密码";
                return jm;
            }

            userModel.passWord = newPassWord;

            var bl = await _sysUserServices.UpdateAsync(userModel);

            jm.code = bl ? 0 : 1;
            jm.msg = bl ? "修改成功" : "修改失败";
            return jm;
        }

        #endregion 用户编辑个人登录账户密码====================================================

        #region 用户编辑个人非安全隐私数据====================================================

        /// <summary>
        ///     用户编辑个人非安全隐私数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> EditLoginUserInfo([FromBody] EditLoginUserInfo entity)
        {
            var jm = new AdminUiCallBack();

            if (entity.trueName.Length > 4)
            {
                jm.msg = "用户真实姓名不能大于4个字符。";
                return jm;
            }

            var userModel = await _sysUserServices.QueryByIdAsync(_user.ID);

            if (!string.IsNullOrEmpty(entity.nickName)) userModel.nickName = entity.nickName;
            if (!string.IsNullOrEmpty(entity.avatar)) userModel.avatar = entity.avatar;
            if (entity.sex > 0) userModel.sex = entity.sex;
            if (!string.IsNullOrEmpty(entity.phone)) userModel.phone = entity.phone;
            if (!string.IsNullOrEmpty(entity.email)) userModel.email = entity.email;
            if (!string.IsNullOrEmpty(entity.introduction)) userModel.introduction = entity.introduction;

            if (!string.IsNullOrEmpty(entity.trueName)) userModel.trueName = entity.trueName;
            if (!string.IsNullOrEmpty(entity.idCard)) userModel.idCard = entity.idCard;
            if (entity.birthday != null) userModel.birthday = entity.birthday;

            userModel.updateTime = DateTime.Now;
            var bl = await _sysUserServices.UpdateAsync(userModel);

            jm.code = bl ? 0 : 1;
            jm.msg = bl ? "修改成功" : "修改失败";
            return jm;
        }

        #endregion 用户编辑个人非安全隐私数据====================================================

        #region 反射获取后端Api的controller和action

        /// <summary>
        ///     反射获取后端Api的controller和action
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public AdminUiCallBack GetAllControllerAndActionByAssembly()
        {
            var jm = new AdminUiCallBack();
            var data = AdminsControllerPermission.GetAllControllerAndActionByAssembly();
            jm.data = data.OrderBy(u => u.name).ToList();
            jm.code = 0;
            jm.msg = "获取成功";
            return jm;
        }

        #endregion 反射获取后端Api的controller和action

        //通用操作=========================================================================

        #region 通用上传接口====================================================

        /// <summary>
        ///     通用上传接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> UploadFiles()
        {
            var jm = new AdminUiCallBack();

            //var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions();

            ////初始化上传参数
            //var maxSize = 1024 * 1024 * filesStorageOptions.MaxSize; //上传大小5M

            //var file = Request.Form.Files["file"];
            //if (file == null)
            //{
            //    jm.msg = "请选择文件";
            //    return jm;
            //}

            //var fileName = file.FileName;
            //var fileExt = Path.GetExtension(fileName).ToLowerInvariant();

            ////检查大小
            //if (file.Length > maxSize)
            //{
            //    jm.msg = "上传文件大小超过限制，最大允许上传" + filesStorageOptions.MaxSize + "M";
            //    return jm;
            //}

            ////检查文件扩展名
            //if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filesStorageOptions.FileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            //{
            //    jm.msg = "上传文件扩展名是不允许的扩展名,请上传后缀名为：" + filesStorageOptions.FileTypes;
            //    return jm;
            //}

            //string url = string.Empty;
            //if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForLocalStorage(filesStorageOptions, fileExt, file);
            //}
            //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForAliYunOSS(filesStorageOptions, fileExt, file);
            //}
            //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForQCloudOSS(filesStorageOptions, fileExt, file);
            //}
            //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForQiNiuKoDo(filesStorageOptions, fileExt, file);
            //}

            //var bl = !string.IsNullOrEmpty(url);
            //jm.code = bl ? 0 : 1;
            //jm.msg = bl ? "上传成功!" : "上传失败";
            //jm.data = new
            //{
            //    fileUrl = url,
            //    src = url
            //};

            return jm;
        }

        #endregion 通用上传接口====================================================

        #region 裁剪Base64上传====================================================

        ///// <summary>
        /////     裁剪Base64上传
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<AdminUiCallBack> UploadFilesFByBase64([FromBody] FMBase64Post entity)
        //{
        //    var jm = new AdminUiCallBack();

        //    //var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions();

        //    //if (string.IsNullOrEmpty(entity.base64))
        //    //{
        //    //    jm.msg = "请上传合法内容";
        //    //    return jm;
        //    //}

        //    entity.base64 = entity.base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
        //    byte[] bytes = Convert.FromBase64String(entity.base64);
        //    MemoryStream memStream = new MemoryStream(bytes);

        //    string url = string.Empty;
        //    //if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString())
        //    //{
        //    //    url = _toolsServices.UpLoadBase64ForLocalStorage(filesStorageOptions, memStream);
        //    //}
        //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString())
        //    //{
        //    //    //上传到阿里云
        //    //    url = await _toolsServices.UpLoadBase64ForAliYunOSS(filesStorageOptions, memStream);
        //    //}
        //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString())
        //    //{
        //    //    //上传到腾讯云OSS
        //    //    url = _toolsServices.UpLoadBase64ForQCloudOSS(filesStorageOptions, bytes);
        //    //}
        //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString())
        //    //{
        //    //    //上传到七牛云kodo
        //    //    url = _toolsServices.UpLoadBase64ForQiNiuKoDo(filesStorageOptions, bytes);
        //    //}
        //    var bl = !string.IsNullOrEmpty(url);
        //    jm.code = bl ? 0 : 1;
        //    jm.msg = bl ? "上传成功!" : "上传失败";
        //    jm.data = new
        //    {
        //        fileUrl = url,
        //        src = url
        //    };

        //    return jm;
        //}

        #endregion 裁剪Base64上传====================================================

        #region CKEditor编辑上传接口====================================================

        /// <summary>
        ///     CKEditor编辑上传接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<CKEditorUploadedResult> CkEditorUploadFiles()
        {
            var jm = new CKEditorUploadedResult();

            //var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions();

            ////初始化上传参数
            //var maxSize = 1024 * 1024 * filesStorageOptions.MaxSize; //上传大小5M

            //var file = Request.Form.Files["upload"];
            //if (file == null)
            //{
            //    jm.error.message = "请选择文件";
            //    return jm;
            //}

            //var fileName = file.FileName;
            //var fileExt = Path.GetExtension(fileName).ToLowerInvariant();

            ////检查大小
            //if (file.Length > maxSize)
            //{
            //    jm.error.message = "上传文件大小超过限制，最大允许上传" + filesStorageOptions.MaxSize + "M";
            //    return jm;
            //}

            ////检查文件扩展名
            //if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filesStorageOptions.FileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            //{
            //    jm.error.message = "上传文件扩展名是不允许的扩展名,请上传后缀名为：" + filesStorageOptions.FileTypes;
            //    return jm;
            //}

            //string url = string.Empty;
            //if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForLocalStorage(filesStorageOptions, fileExt, file);
            //}
            //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString())
            //{
            //    //上传到阿里云
            //    url = await _toolsServices.UpLoadFileForAliYunOSS(filesStorageOptions, fileExt, file);
            //}
            //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForQCloudOSS(filesStorageOptions, fileExt, file);
            //}
            //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString())
            //{
            //    url = await _toolsServices.UpLoadFileForQiNiuKoDo(filesStorageOptions, fileExt, file);
            //}
            //jm.uploaded = !string.IsNullOrEmpty(url) ? 1 : 0;
            //jm.fileName = fileName;
            //jm.url = url;
            return jm;
        }

        #endregion CKEditor编辑上传接口====================================================

        #region 根据id获取商品信息====================================================

        // POST: Api/Tools/GetGoodsByIds
        /// <summary>
        ///     根据id获取商品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("根据id获取商品信息")]
        public async Task<AdminUiCallBack> GetGoodsByIds([FromBody] FMArrayIntIds entity)
        {
            var jm = new AdminUiCallBack();

            // var list = await _coreCmsGoodsServices.QueryByIDsAsync(entity.id);
            jm.code = 0;
            jm.data = null;

            return jm;
        }

        #endregion 根据id获取商品信息====================================================

        #region 后台生成小程序码============================================================

        // POST: Api/CoreCmsForm/GetCreate
        /// <summary>
        ///     后台生成小程序码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("后台生成小程序码")]
        public async Task<AdminUiCallBack> GetFormWxCode([FromBody] FMIntId entity)
        {
            //返回数据
            var jm = new AdminUiCallBack { code = 0 };

            ////var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions();

            ////var formModel = await _coreCmsFormServices.QueryByIdAsync(entity.id);
            ////if (formModel == null)
            ////{
            ////    jm.code = 1;
            ////    jm.msg = "不存在此信息";
            ////    return jm;
            ////}

            //var path = "pages/form/details/details?id=" + entity.id;

            //var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
            //var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
            //var request = new CgibinWxaappCreateWxaQrcodeRequest();
            //request.AccessToken = accessToken;
            //request.Path = path;

            //var response = await client.ExecuteCgibinWxaappCreateWxaQrcodeAsync(request);
            //if (response.IsSuccessful())
            //{
            //    var memStream = new MemoryStream(response.RawBytes);

            //    string url = string.Empty;
            //    //if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString())
            //    //{
            //    //    url = _toolsServices.UpLoadBase64ForLocalStorage(filesStorageOptions, memStream);
            //    //}
            //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString())
            //    //{
            //    //    //上传到阿里云
            //    //    url = await _toolsServices.UpLoadBase64ForAliYunOSS(filesStorageOptions, memStream);
            //    //}
            //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString())
            //    //{
            //    //    //上传到腾讯云OSS
            //    //    url = _toolsServices.UpLoadBase64ForQCloudOSS(filesStorageOptions, response.RawBytes);
            //    //}
            //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString())
            //    //{
            //    //    //上传到七牛云kodo
            //    //    url = _toolsServices.UpLoadBase64ForQiNiuKoDo(filesStorageOptions, response.RawBytes);
            //    //}

            //    var bl = !string.IsNullOrEmpty(url);
            //    jm.code = bl ? 0 : 1;
            //    jm.msg = bl ? "上传成功!" : "上传失败";
            //    jm.data = new
            //    {
            //        fileUrl = url,
            //        src = url
            //    };
            //}
            //else
            //{
            //    jm.code = 1;
            //    jm.msg = response.ErrorMessage;
            //}
            //jm.otherData = response;

            return jm;
        }

        #endregion 后台生成小程序码============================================================

        #region 后台生成预览页面设计小程序码============================================================

        // POST: Api/Tools/GetPageWxCode
        /// <summary>
        ///     后台生成预览页面设计小程序码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("后台生成小程序码")]
        public async Task<AdminUiCallBack> GetPageWxCode([FromBody] FMStringId entity)
        {
            //返回数据
            var jm = new AdminUiCallBack { code = 0 };

            ////var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions();

            ////var pageModel = await _pagesServices.QueryByClauseAsync(p => p.code == entity.id);
            ////if (pageModel == null)
            ////{
            ////    jm.code = 1;
            ////    jm.msg = "不存在此信息";
            ////    return jm;
            ////}

            //var path = "pages/index/custom/custom?pageCode=" + entity.id;

            //var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
            //var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
            //var request = new CgibinWxaappCreateWxaQrcodeRequest();
            //request.AccessToken = accessToken;
            //request.Path = path;

            //var response = await client.ExecuteCgibinWxaappCreateWxaQrcodeAsync(request);
            //if (response.IsSuccessful())
            //{
            //    var memStream = new MemoryStream(response.RawBytes);

            //    string url = string.Empty;
            //    //if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString())
            //    //{
            //    //    url = _toolsServices.UpLoadBase64ForLocalStorage(filesStorageOptions, memStream);
            //    //}
            //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString())
            //    //{
            //    //    //上传到阿里云
            //    //    url = await _toolsServices.UpLoadBase64ForAliYunOSS(filesStorageOptions, memStream);
            //    //}
            //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString())
            //    //{
            //    //    //上传到腾讯云OSS
            //    //    url = _toolsServices.UpLoadBase64ForQCloudOSS(filesStorageOptions, response.RawBytes);
            //    //}
            //    //else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString())
            //    //{
            //    //    //上传到七牛云kodo
            //    //    url = _toolsServices.UpLoadBase64ForQiNiuKoDo(filesStorageOptions, response.RawBytes);
            //    //}

            //    var bl = !string.IsNullOrEmpty(url);
            //    jm.code = bl ? 0 : 1;
            //    jm.msg = bl ? "上传成功!" : "上传失败";
            //    jm.data = new
            //    {
            //        fileUrl = url,
            //        src = url
            //    };
            //}
            //else
            //{
            //    jm.code = 1;
            //    jm.msg = response.ErrorMessage;
            //}
            //jm.otherData = response;

            return jm;
        }

        #endregion 后台生成预览页面设计小程序码============================================================

        //通用页面获取=========================================================================

        #region 获取商品列表====================================================

        // POST: Api/Tools/GetGoods
        /// <summary>
        ///     获取商品列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取商品列表")]
        public async Task<AdminUiCallBack> GetGoods()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<CoreCmsGoods>();
            //where = where.And(p => p.isMarketable);
            ////商品编码 nvarchar
            //var bn = Request.Form["bn"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(bn)) where = where.And(p => p.bn.Contains(bn));
            ////商品名称 nvarchar
            //var name = Request.Form["name"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(name)) where = where.And(p => p.name.Contains(name));
            //where = where.And(p => p.isDel == false);
            ////获取数据
            //var list = await _coreCmsGoodsServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc,
            //    pageCurrent, pageSize);
            ////返回数据

            //var newObj = list.Select(p => new
            //{
            //    p.id,
            //    image = !string.IsNullOrEmpty(p.images) ? p.images.Split(",")[0] : "/static/images/common/empty.png",
            //    p.images,
            //    p.price,
            //    p.name,
            //    p.stock
            //}).ToList();

            //jm.data = newObj;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取商品列表====================================================

        #region 根据商品序列获取货品数据====================================================

        // POST: Api/Tools/GetProducts
        /// <summary>
        ///     根据商品序列获取货品数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取商品列表")]
        public async Task<AdminUiCallBack> GetProducts(FMIntId entity)
        {
            var jm = new AdminUiCallBack();

            //var list = await _productsServices.GetProducts(entity.id);

            //jm.code = 0;
            //jm.data = list;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 根据商品序列获取货品数据====================================================

        #region 获取文章列表============================================================

        // POST: Api/Tools/GetArticles
        /// <summary>
        ///     获取文章列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取文章列表")]
        public async Task<AdminUiCallBack> GetArticles()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<CoreCmsArticle>();

            ////标题 nvarchar
            //var title = Request.Form["title"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(title)) where = where.And(p => p.title.Contains(title));

            ////获取数据
            //var list = await _coreCmsArticleServices.QueryPageAsync(where, p => p.id, OrderByType.Asc, pageCurrent,
            //    pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取文章列表============================================================

        #region 获取智能表单列表============================================================

        // POST: Api/Tools/GetForms
        /// <summary>
        ///     获取智能表单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取智能表单列表")]
        public async Task<AdminUiCallBack> GetForms()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<CoreCmsForm>();

            ////表单名称 nvarchar
            //var name = Request.Form["name"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(name)) where = where.And(p => p.name.Contains(name));

            ////获取数据
            //var list = await _coreCmsFormServices.QueryPageAsync(where, p => p.id, OrderByType.Desc, pageCurrent,
            //    pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取智能表单列表============================================================

        #region 获取文章分类列表============================================================

        // POST: Api/Tools/GetArticleTypes
        /// <summary>
        ///     获取文章分类列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取文章分类列表")]
        public async Task<AdminUiCallBack> GetArticleTypes()
        {
            var jm = new AdminUiCallBack();

            ////获取数据
            //var list = await _coreCmsArticleTypeServices.QueryAsync();
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取文章分类列表============================================================

        #region 获取公告列表============================================================

        // POST: Api/Tools/GetNotices
        /// <summary>
        ///     获取公告列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取公告列表")]
        public async Task<AdminUiCallBack> GetNotices()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<CoreCmsNotice>();

            ////公告标题 nvarchar
            //var title = Request.Form["title"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(title)) where = where.And(p => p.title.Contains(title));
            ////获取数据
            //var list = await _coreCmsNoticeServices.QueryPageAsync(where, p => p.id, OrderByType.Desc, pageCurrent,
            //    pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取公告列表============================================================

        #region 供tag标签选择拼团商品的时候使用============================================================

        // POST: Api/Tools/GetPingTuans
        /// <summary>
        ///     供tag标签选择拼团商品的时候使用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        public async Task<AdminUiCallBack> TagPinTuan()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<TagPinTuanResult>();

            //var dt = DateTime.Now;
            //where = where.And(p => p.isStatusOpen == true && p.startTime < dt && p.endTime > dt);

            ////活动名称 nvarchar
            //var name = Request.Form["name"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(name)) where = where.And(p => p.name.Contains(name));

            ////获取数据
            //var list = await _coreCmsPinTuanRuleServices.QueryTagPinTuanPageAsync(where, p => p.id, OrderByType.Desc, pageCurrent, pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 供tag标签选择拼团商品的时候使用============================================================

        #region 供tag标签选择合法团购秒杀的时候使用============================================================

        // POST: Api/Tools/TagPromotions
        /// <summary>
        ///     供tag标签选择合法团购秒杀的时候使用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("供tag标签选择合法团购秒杀的时候使用")]
        public async Task<AdminUiCallBack> TagPromotions()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<CoreCmsPromotion>();

            //var dt = DateTime.Now;
            //where = where.And(p => p.isEnable == true && p.isDel == false && p.startTime < dt && p.endTime > dt && p.type == (int)GlobalEnumVars.PromotionType.Seckill);

            ////促销名称 nvarchar
            //var name = Request.Form["name"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(name)) where = where.And(p => p.name.Contains(name));

            //where = where.And(p => p.isDel == false);
            ////获取数据
            //var list = await _coreCmsPromotionServices.QueryPageAsync(where, p => p.id, OrderByType.Desc, pageCurrent,
            //    pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 供tag标签选择合法团购秒杀的时候使用============================================================

        #region 供tag标签选择服务卡的时候使用============================================================

        // POST: Api/Tools/TagServices
        /// <summary>
        ///     供tag标签选择服务卡的时候使用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        public async Task<AdminUiCallBack> TagServices()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);

            //var dt = DateTime.Now;
            //var where = PredicateBuilder.True<CoreCmsServices>();

            //where = where.And(p => p.status == (int)GlobalEnumVars.ServicesStatus.Shelve);
            //where = where.And(p => p.amount > 0);
            //where = where.And(p => p.startTime < dt && p.endTime > dt);

            ////服务名称 nvarchar
            //var title = Request.Form["title"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(title)) where = where.And(p => p.title.Contains(title));

            ////获取数据
            //var list = await _servicesServices.TagQueryPageAsync(where, p => p.createTime, OrderByType.Desc, pageCurrent, pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 供tag标签选择服务卡的时候使用============================================================

        #region 获取促销列表============================================================

        // POST: Api/Tools/GetPromotions
        /// <summary>
        ///     获取促销列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取促销列表")]
        public async Task<AdminUiCallBack> GetPromotions()
        {
            var jm = new AdminUiCallBack();
            //var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            //var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            //var where = PredicateBuilder.True<CoreCmsPromotion>();

            ////序列 int
            //var types = Request.Form["types"].FirstOrDefault().ObjectToInt(0);
            //if (types == 1)
            //    where = where.And(p => p.type == 1);
            //else if (types == 2)
            //    where = where.And(p => p.type == 2);
            //else if (types == 3) where = where.And(p => p.type == 3 || p.type == 4);
            ////促销名称 nvarchar
            //var name = Request.Form["name"].FirstOrDefault();
            //if (!string.IsNullOrEmpty(name)) where = where.And(p => p.name.Contains(name));

            //where = where.And(p => p.isDel == false);
            ////获取数据
            //var list = await _coreCmsPromotionServices.QueryPageAsync(where, p => p.id, OrderByType.Desc, pageCurrent,
            //    pageSize);
            ////返回数据
            //jm.data = list;
            //jm.code = 0;
            //jm.count = list.TotalCount;
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取促销列表============================================================

        #region 获取区域信息=======================================================

        /// <summary>
        ///     获取区域信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetArea()
        {
            var jm = new WebApiCallBack();

            //var ischecked = Request.Form["ischecked"].FirstOrDefault().ObjectToInt(0);
            //var nodeId = Request.Form["nodeId"].FirstOrDefault().ObjectToInt(0);
            //var idsStr = Request.Form["ids"].FirstOrDefault();

            //var ids = new List<PostAreasTreeNode>();
            //if (!string.IsNullOrEmpty(idsStr)) ids = JsonConvert.DeserializeObject<List<PostAreasTreeNode>>(idsStr);
            //var areaTrees = await _areaServices.GetTreeArea(ids, nodeId, ischecked);

            //jm.status = true;
            //jm.data = areaTrees;
            //jm.msg = ids.Count.ToString();

            return jm;
        }

        #endregion 获取区域信息=======================================================

        #region 物流查询接口=======================================================

        /// <summary>
        ///     物流查询接口
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public async Task<WebApiCallBack> LogisticsByApi([FromBody] FMApiLogisticsByApiPost entity)
        //{
        //    var jm = new WebApiCallBack();

        //    if (string.IsNullOrEmpty(entity.code) || string.IsNullOrEmpty(entity.no))
        //    {
        //        jm.code = 1;
        //        jm.msg = GlobalErrorCodeVars.Code13225;
        //        return jm;
        //    }

        //    var systemLogistics = SystemSettingDictionary.GetSystemLogistics();
        //    foreach (var p in systemLogistics)
        //    {
        //        if (entity.code == p.sKey)
        //        {
        //            jm.msg = p.sDescription + "不支持轨迹查询";
        //            return jm;
        //        }
        //    }

        //    jm = await _logisticsServices.ExpressPoll(entity.code, entity.no, entity.mobile);
        //    return jm;
        //}

        #endregion 物流查询接口=======================================================

        //用户相关=========================================================================

        #region 根据用户权限获取对应左侧菜单列表====================================================

        /// <summary>
        ///     根据用户权限获取对应左侧菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AdminUiCallBack> GetNavs()
        {
            _logger.LogError("te");
            var jm = new AdminUiCallBack();

            //先获取用户关联角色
            var roles = await _sysUserRoleServices.QueryListByClauseAsync(p => p.userId == _user.ID);
            if (roles.Any())
            {
                var roleIds = roles.Select(p => p.roleId).ToList();
                var sysRoleMenu = await _sysRoleMenuServices.QueryListByClauseAsync(p => roleIds.Contains(p.roleId));

                var menuIds = sysRoleMenu.Select(p => p.menuId).ToList();

                var where = PredicateBuilder.True<SysMenu>();
                where = where.And(p => p.deleted == false && p.hide == false && p.menuType == 0);
                where = where.And(p => menuIds.Contains(p.id));

                var navs = await _sysMenuServices.QueryListByClauseAsync(where, p => p.sortNumber, OrderByType.Asc);
                var menus = GetMenus(navs, 0);

                jm.data = menus;
            }

            jm.msg = "数据获取正常";
            jm.code = 0;

            return jm;
        }

        /// <summary>
        ///     迭代方法
        /// </summary>
        /// <param name="oldNavs"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static List<AdminUiMenu> GetMenus(List<SysMenu> oldNavs, int parentId)
        {
            var childTree = new List<AdminUiMenu>();
            if (parentId == 0)
            {
                var topMenu = new AdminUiMenu { title = "主页", icon = "layui-icon-home", name = "HomePanel" };
                var list = new List<AdminUiMenu>
                {
                    new AdminUiMenu
                        {title = "控制台", jump = "/", name = "controllerPanel", list = new List<AdminUiMenu>()}
                };
                topMenu.list = list;
                childTree.Add(topMenu);
            }

            var model = oldNavs.Where(p => p.parentId == parentId).ToList();
            foreach (var item in model)
            {
                var menu = new AdminUiMenu
                {
                    name = item.identificationCode,
                    title = item.menuName,
                    icon = item.menuIcon,
                    jump = !string.IsNullOrEmpty(item.path) ? item.path : null
                };
                childTree.Add(menu);
                menu.list = GetMenus(oldNavs, item.id);
            }

            return childTree;
        }

        #endregion 根据用户权限获取对应左侧菜单列表====================================================

        //后端首页使用数据

        #region 获取最近登录日志============================================================

        // POST: Api/Tools/GetSysLoginRecord
        /// <summary>
        ///     获取最近登录日志
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取最近登录日志")]
        public async Task<AdminUiCallBack> GetSysLoginRecord()
        {
            var jm = new AdminUiCallBack();

            //获取数据
            var list = await _sysLoginRecordServices.QueryPageAsync(p => p.id > 0, p => p.createTime, OrderByType.Desc, 1, 10);
            //返回数据
            jm.data = list;
            jm.code = 0;
            jm.count = list.TotalCount;
            jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取最近登录日志============================================================

        #region 获取全局Nlog日志============================================================

        // POST: Api/Tools/GetSysNLogRecords
        /// <summary>
        ///     获取全局Nlog日志
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取全局Nlog日志")]
        public async Task<AdminUiCallBack> GetSysNLogRecords()
        {
            var jm = new AdminUiCallBack();
            //获取数据
            var list = await _sysNLogRecordsServices.QueryPageAsync(p => p.id > 0, p => p.id, OrderByType.Desc, 1, 10);
            //返回数据
            jm.data = list;
            jm.code = 0;
            jm.count = list.TotalCount;
            jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取全局Nlog日志============================================================

        #region 获取7天订单情况数据统计============================================================

        // POST: Api/Tools/GetOrdersStatistics
        /// <summary>
        ///     获取7天订单情况数据统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取7天订单情况数据统计")]
        public async Task<AdminUiCallBack> GetOrdersStatistics()
        {
            var jm = new AdminUiCallBack();

            //var data = new List<string>() { "已支付", "已发货" };
            //var legend = new Legend();
            //legend.data = data;

            //var paymentsStatistics = await _paymentsServices.Statistics();
            //var billDeliveryStatistics = await _billDeliveryServices.Statistics();

            //var xAxis = new List<XAxis>();
            //var xItem = new XAxis();
            //xItem.type = "category";
            //xItem.data = paymentsStatistics.Select(p => p.day).ToList();

            //for (int i = 0; i < xItem.data.Count; i++)
            //{
            //    xItem.data[i] = Convert.ToDateTime(xItem.data[i]).ToString("d日");
            //}
            //xAxis.Add(xItem);

            //var series = new List<SeriesDataIntItem>
            //{
            //    new SeriesDataIntItem()
            //    {
            //        name = "已支付", type = "line", data = paymentsStatistics.Select(p => p.nums).ToList()
            //    },
            //    new SeriesDataIntItem()
            //    {
            //        name = "已发货", type = "line", data = billDeliveryStatistics.Select(p => p.nums).ToList()
            //    }
            //};

            //jm.code = 0;
            //jm.data = new
            //{
            //    legend,
            //    xAxis,
            //    series
            //};

            ////返回数据
            //jm.msg = "数据调用成功!";
            return jm;
        }

        #endregion 获取7天订单情况数据统计============================================================
    }
}